package cn.deschen.eshop.auth.validate.sms;

import cn.deschen.eshop.auth.properties.SecurityProperties;
import cn.deschen.eshop.auth.properties.SmsCodeProperties;
import cn.deschen.eshop.auth.validate.AbstractValidateCodeProcessor;
import cn.deschen.eshop.auth.validate.ValidateCode;
import cn.deschen.eshop.auth.validate.ValidateCodeProcessor;
import cn.deschen.eshop.common.enums.GlobalEnum;
import cn.deschen.eshop.common.exception.GlobalException;
import cn.deschen.eshop.common.utils.HttpUtils;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author hanbin_chen
 * @Description 图片验证码处理器
 * @DateTime 2021/1/14 13:41
 * @Version V1.0.0
 */
@Slf4j
@Component
public class SmsValidateCodeProcessor extends AbstractValidateCodeProcessor {

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    protected void sendCode(ServletWebRequest request, ValidateCode code) {
        SmsCodeProperties smsCodeProperties = securityProperties.getCode().getSms();
        Map<String, String> headers = new HashMap<>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + smsCodeProperties.getAppcode());
        // 请求参数
        Map<String, String> querys = new HashMap<>();
        String mobile = "";
        try {
            mobile = ServletRequestUtils.getStringParameter(request.getRequest(), smsCodeProperties.getReqKeyForCache());
        } catch (Exception e) {
            log.error("【权限模块】获取请求参数失败");
            throw new GlobalException(GlobalEnum.OPERATION_ERROR, "获取请求参数失败");
        }
        querys.put("mobile", mobile);
        querys.put("param", "code:" + code.getCode());
        querys.put("tpl_id", smsCodeProperties.getTpl_id());

        Map<String, String> bodys = new HashMap<>();
        try {
            HttpResponse response = HttpUtils.doPost(
                    smsCodeProperties.getHost(), smsCodeProperties.getPath(), smsCodeProperties.getMethod(), headers, querys, bodys);
            //获取response的body
            Map<String, String> resMap = JSONUtil.toBean(
                    EntityUtils.toString(
                            response.getEntity()), new TypeReference<Map<String, String>>() {
                    }, false);
            // 判断响应是否成功
            if (!resMap.get("return_code").equals("00000")) {
                log.error("【权限模块】短信验证码发送失败");
                throw new GlobalException(GlobalEnum.OPERATION_ERROR, "短信验证码发送失败");
            }
        } catch (Exception e) {
            log.error("【权限模块】短信验证码发送失败");
            throw new GlobalException(GlobalEnum.OPERATION_ERROR, "短信验证码发送失败," + e.getMessage());
        }
    }
}
